package com.ywk.springCore.lifeCycle.handRegistryBean;

import com.ywk.springCore.lifeCycle.core.Card;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.stereotype.Component;

/**
 * BeanDefinitionRegistryPostProcessor实现类会优先于 BeanPostProcessor 和 BeanFactoryPostProcessor 执行（所以此类是个特殊的bean）
 * 手动注册bean的实现类
 *
 * @Author zbh
 * @Date 2023/4/10
 */
@Component
@Data
@Slf4j
public class MyBeanDefinitionRegistry implements BeanDefinitionRegistryPostProcessor {

    Integer id=1;

    /**
     * BeanDefinitionRegistryPostProcessor
     * 手动注册bean
     * @param beanDefinitionRegistry
     * @throws BeansException
     */
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        log.info("执行BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry进行手动注册handBeanRegistry");
        //注册并填充属性（这里的属性填充是在实例化后初始化前注入的，与@Value类似）
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(HandBeanRegistry.class);
        builder.addPropertyValue("id", 1);
        builder.addPropertyValue("card",new Card(11));
        beanDefinitionRegistry.registerBeanDefinition("handBeanRegistry",builder.getBeanDefinition());
    }

    /**
     *BeanDefinitionRegistryPostProcessor
     * @param configurableListableBeanFactory
     * @throws BeansException
     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

    }

}
